// @author: Nguyen Vu Hung
	$(document)
			.ready(
					function() {
						$("#chkTimeOut")
								.click(
										function() {
											var checkbox = document
													.getElementById("chkTimeOut");
											if (checkbox.checked == true) {
												document
														.getElementById("txtTimeOut").value = "30";
											} else {
												document
														.getElementById("txtTimeOut").value = "0";
											}
										});

						$("#btnShowAdvanceSearch").click(function() {
							$("#panelAdvanceSearch").slideDown("slow");
						});

						$("#btnHideAdvanceSearch").click(function() {
							$("#panelAdvanceSearch").slideUp("slow");
						});

						$
								.get(
										'bloglogonautocomplete.json',
										function(data) {
											$("#txtname").typeahead({
												source : data
											});
										}, 'json');
					});

	/* SHOW ALL */
	var pager;

	$(function() {
		var urlJson = "bloglogonjson.json";
		showTable(urlJson);
	});

	function showTable(string) {
		$("#mytable > tbody").html("");
		$("#tblReport > tbody").html("");

		$("#tableContent")
				.append(
						"<div class=\"pagination pagination-centered\" id=\"pageNavPosition\"></div>");

		$.getJSON(string, {
			ajax : 'true'
		}, function(data) {
			var length = data.length;
			if (length == 0) {
				var content = '<tr>';
				content += '<td colspan="6" style="text-align:center;">';
				content += 'No matching blog logon transactions found';
				content += '</td>';
				content += '</tr>';
				$("#mytable > tbody").append(content);
				$("#tblReport > tbody").append(content);
			} else {
			for (var i = 0; i < length; i++) {
				var content = "<tr>";
				content += "<td>" + (i + 1) + "</td>";
				content += "<td>" + data[i].userName + "</td>";
				content += "<td>" + data[i].fullName + "</td>";
				content += "<td>" + data[i].role + "</td>";
				content += "<td>" + data[i].task + " </td>";
				content += "<td>" + data[i].time + "</td>";
				content += "</tr>";
				$("#mytable > tbody").append(content);
				$("#tblReport > tbody").append(content);
			}
			}
			pager = new Pager('mytable', 20);
			pager.init();
			pager.showPageNav('pager', 'pageNavPosition');
			pager.showPage(1);
		});
	}

	/* SEARCH BY NAME */

	function searchbasic() {
		var name = $('#txtname').val();
		var urlJson = "searchnamebloglogon/"
				+ name + ".json";
		$.ajax({
			type : "GET",
			url : urlJson,
			success : function(response) {
				showTable(urlJson);
			},
		});
	}

	/* SEARCH ADVANCED */

	function searchadvanced() {
		var username = $('#username').val();
		var cmbRole = $('#cmbRole').val();
		var datefrom = $('#datefrom').val();
		var dateto = $('#dateto').val();

		if (cmbRole == "") {
			cmbRole = "null";
		}
		if (username == "") {
			username = "null";
		}
		if (datefrom == "") {
			datefrom = "null";
		}
		if (dateto == "") {
			dateto = "null";
		}

		var urlJson = "advancedSearchTimeUsed/"
				+ username
				+ '/'
				+ cmbRole
				+ '/'
				+ datefrom
				+ '/'
				+ dateto
				+ ".json";
		$.ajax({
			type : "GET",
			url : urlJson,
			success : function(response) {
				showTable(urlJson);
			},
		});
	}

	// Print
	function printPage(id) {
		var html = "<html><head>";
		html += "<link type='text/css' href='resources/main/css/bootstrap.min.css' rel='stylesheet'>";
		html += "<link type='text/css' href='resources/main/css/theme.css' rel='stylesheet'></head>";

		html += document.getElementById(id).innerHTML;
		html += "</html>";

		var printWin = window
				.open('', '',
						'left=0,top=0,width=1000,height=800,toolbar=0,scrollbars=0,status  =0');
		printWin.document.write(html);
		printWin.document.close();
		printWin.focus();

	}

	// PDF
	function exportPDF() {
		var username = $('#username').val();
		var cmbRole = $('#cmbRole').val();
		var datefrom = $('#datefrom').val();
		var dateto = $('#dateto').val();

		if (cmbRole == "") {
			cmbRole = "null";
		}
		if (username == "") {
			username = "null";
		}
		if (datefrom == "") {
			datefrom = "null";
		}
		if (dateto == "") {
			dateto = "null";
		}

		var url = 'generatePDFBlogLogon.html?username=' + username
				+ '&cmbRole=' + cmbRole + '&datefrom=' + datefrom + '&dateto='
				+ dateto;

		window.open(url);
	}